package com.smart.home.service.impl;

import com.smart.home.dto.Devices.DevicesTypeDto;
import com.smart.home.entity.DeviceTypes;
import com.smart.home.entity.Devices;
import com.smart.home.mapper.DeviceTypeMapper;
import com.smart.home.mapper.DevicesMapper;
import com.smart.home.service.DevicesTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;

@Service
@Slf4j
public class DevicesTypeImpl implements DevicesTypeService {

    @Autowired
    private DeviceTypeMapper deviceTypeMapper;

    @Autowired
    private DevicesMapper devicesMapper;
    /**
     * 添加设备类型
     * @param devicesTypeDto
     * @return
     */
    @Override
    public Long addDevice(DevicesTypeDto devicesTypeDto) {
        DeviceTypes deviceTypes = new DeviceTypes();
        BeanUtils.copyProperties(devicesTypeDto, deviceTypes);
        deviceTypeMapper.addDevice(deviceTypes);

        log.info("添加设备类型成功");

        return deviceTypes.getId();
    }

    /**
     * 修改设备类型
     *
     * @param devicesTypeDto
     * @param deviceTypeId
     * @return
     */
    @Override
    public Long updateDevice(DevicesTypeDto devicesTypeDto, Long deviceTypeId) {
        DeviceTypes deviceTypes = new DeviceTypes();
        BeanUtils.copyProperties(devicesTypeDto, deviceTypes);
        deviceTypeMapper.updateDevice(deviceTypes,deviceTypeId);
        log.info("修改设备类型成功：{}",deviceTypes.getId());
        return deviceTypes.getId();
    }

    /**
     * 查询相关家庭的相关设备类型
     * @param id
     * @return
     */
    @Override
    public List<DeviceTypes> queryDeviceTypes(Long id) {
        List<DeviceTypes> devices = deviceTypeMapper.queryDeviceTypes(id);
        log.info("查询设备类型成功");
        return devices;
    }

    /**
     * 查询设备类型下的设备
     * @param id
     * @return
     */
    @Override
    public List<Devices> queryDevice(Long id) {

        List<Devices> devices = devicesMapper.queryDevice(id);
        Iterator<Devices> iterator = devices.iterator();
        if(id == 0l){
            return devices;
        }
        while (iterator.hasNext()){
            Devices device = iterator.next();
            if(!isOnline(device.getStatus()) || device.getStatus().isEmpty()){
                iterator.remove();
            }
        }

        log.info("查询设备成功");

        return devices;
    }

    /**
     * 移除设备
     * @param id
     * @return
     */
    @Override
    public void removeDevice(Long id) {
        Long typeId = 0l;
        String status = "OFFLINE";
        devicesMapper.updateDeviceStatus(status,id, LocalDateTime.now());
        devicesMapper.removeDevice(id,typeId);
    }

    /**
     * 删除设备类型
     * @param id
     */
    @Override
    public void removeDeviceType(Long id) {
        List<Long> deviceIds = deviceTypeMapper.getDevices(id);
        for (Long deviceId : deviceIds) {
            removeDevice(deviceId);
        }
        deviceTypeMapper.removeDeviceType(id);
        log.info("删除设备类型成功");
    }

    /**
     * 修改设备类型
     * @param DeviceId
     * @param deviceTypeId
     */
    @Override
    public void changeDeviceType(Long DeviceId, Long deviceTypeId) {
        String status = "ONLINE";
        devicesMapper.updateDeviceStatus(status,DeviceId,LocalDateTime.now());
        devicesMapper.removeDevice(DeviceId,deviceTypeId);
        log.info("设备类型修改成功");
    }



    //判断设备是否在线
    private boolean isOnline(String status){
        if("ONLINE".equals(status)){
            return true;
        }
        return false;
    }
}
